package com.kekezu.kppw.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.tsz.afinal.FinalDb;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.widget.Toast;

import com.kekezu.kppw.bean.UserBean;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;
import com.kekezu.kppw.utils.StrFormat;

@SuppressWarnings("deprecation")
public class BankData {
	static FinalDb db;

	/**
	 * 获取用户认证的银行卡列表信息
	 * 
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getBankList(Context context) {
		ArrayList<HashMap<String, Object>> bankList = new ArrayList<HashMap<String, Object>>();
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		String urlString = ConfigInc.getServiceAdress(context)
				+ "auth/bankList?token=" + users.get(0).getToken();
		HttpUtils httpUtils = HttpUtils.getInstance();

		try {
			String strJson = httpUtils.httpGet(urlString, context);

			JSONObject jsonObject = new JSONObject(strJson);

			String resString = jsonObject.getString("code");
			JSONArray jsonArray = jsonObject.getJSONArray("data");
			// Log.e("rescode", resString);
			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);

					map.put("id", itemJsonObject.getString("id"));
					map.put("uid", itemJsonObject.getString("uid"));
					map.put("bank_name", itemJsonObject.getString("bank_name"));

					map.put("deposit_name",
							itemJsonObject.getString("deposit_name"));
					map.put("account", itemJsonObject.getString("bank_account"));

					map.put("bank_img", itemJsonObject.getString("bank_img"));
					map.put("pay_to_user_cash",
							itemJsonObject.getString("pay_to_user_cash"));
					map.put("user_get_cash",
							itemJsonObject.getString("user_get_cash"));

					map.put("status", itemJsonObject.getString("status"));

					if (itemJsonObject.getString("status").equals("0")) {
						map.put("status_string", "待打款");
					} else if (itemJsonObject.getString("status").equals("1")) {
						map.put("status_string", "确认金额");
					} else if (itemJsonObject.getString("status").equals("2")) {
						map.put("status_string", "已认证");
					} else if (itemJsonObject.getString("status").equals("3")) {
						map.put("status_string", "认证失败");
					} else if (!StrFormat.formatStr(itemJsonObject
							.getString("status"))) {
						map.put("status_string", "异常");
					}
					map.put("status", itemJsonObject.getString("status"));
					map.put("username", itemJsonObject.getString("username"));

					map.put("deposit_area",
							itemJsonObject.getString("deposit_area"));

					bankList.add(map);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bankList;
	}

	/**
	 * 获取银行认证可绑定的银行卡名称
	 * 
	 * @param context
	 * @return
	 */
	public static List<String> getBankName(Context context) {
		List<String> banknamelist = new ArrayList<>();
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		String urlString = ConfigInc.getServiceAdress(context)
				+ "auth/getBankAuth?token=" + users.get(0).getToken();
		HttpUtils httpUtils = HttpUtils.getInstance();

		try {
			String strJson = httpUtils.httpGet(urlString, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONArray jsonArray = new JSONArray(
						jsonObject.getString("data"));
				for (int i = 0; i < jsonArray.length(); i++) {
					banknamelist.add(jsonArray.get(i).toString());
				}
			} else {
				Toast.makeText(context, "验证错误信息", Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return banknamelist;
	}

	/**
	 * 银行卡认证
	 * 
	 * @param depositName
	 * @param bankname
	 * @param province
	 * @param bankAccount
	 * @param confirmBankAccount
	 * @param city
	 * @param context
	 */
	public static String[] addBank(String depositName, String bankname,
			String bankAccount, String confirmBankAccount, Context context) {
		String str[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "auth/bankAuth";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("depositName", depositName));
		params.add(new BasicNameValuePair("bankname", bankname));
		params.add(new BasicNameValuePair("bankAccount", bankAccount));
		params.add(new BasicNameValuePair("confirmBankAccount",
				confirmBankAccount));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 验证支付宝认证金额
	 * 
	 * @param strCash
	 * @param strID
	 * @param context
	 */
	public static String[] verifyBankAuthCash(String strCash, String strID,
			Context context) {
		String str[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "auth/verifyBankAuthCash";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));
		params.add(new BasicNameValuePair("cash", strCash));
		params.add(new BasicNameValuePair("auth_id", strID));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

}
